package com.example.myapplication1.ui.profile;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;

import com.example.myapplication1.R;
import com.example.myapplication1.db.DatabaseHelper;
import com.example.myapplication1.utils.PreferenceManager;
import com.google.android.material.textfield.TextInputEditText;

public class ChangePasswordFragment extends Fragment {
    private TextInputEditText oldPasswordInput;
    private TextInputEditText newPasswordInput;
    private TextInputEditText confirmPasswordInput;
    private DatabaseHelper databaseHelper;
    private PreferenceManager preferenceManager;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        databaseHelper = new DatabaseHelper(requireContext());
        preferenceManager = new PreferenceManager(requireContext());
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_change_password, container, false);

        // 初始化视图
        oldPasswordInput = view.findViewById(R.id.old_password);
        newPasswordInput = view.findViewById(R.id.new_password);
        confirmPasswordInput = view.findViewById(R.id.confirm_password);
        Button confirmButton = view.findViewById(R.id.confirm_button);
        ImageButton backButton = view.findViewById(R.id.back_button);

        // 设置返回按钮点击事件
        backButton.setOnClickListener(v -> {
            try {
                // 使用Navigation组件返回到个人中心
                Navigation.findNavController(view).navigate(R.id.action_changePassword_to_profile);
            } catch (Exception e) {
                // 如果导航失败，回退到上一个Fragment
                requireActivity().getSupportFragmentManager().popBackStack();
            }
        });

        // 设置确认按钮点击事件
        confirmButton.setOnClickListener(v -> changePassword(view));

        return view;
    }

    private void changePassword(View view) {
        String oldPassword = oldPasswordInput.getText().toString().trim();
        String newPassword = newPasswordInput.getText().toString().trim();
        String confirmPassword = confirmPasswordInput.getText().toString().trim();

        // 输入验证
        if (TextUtils.isEmpty(oldPassword) || TextUtils.isEmpty(newPassword) || TextUtils.isEmpty(confirmPassword)) {
            Toast.makeText(requireContext(), "请填写所有密码字段", Toast.LENGTH_SHORT).show();
            return;
        }

        // 验证新密码一致性
        if (!newPassword.equals(confirmPassword)) {
            Toast.makeText(requireContext(), "新密码与确认密码不一致", Toast.LENGTH_SHORT).show();
            return;
        }

        // 验证新旧密码不能相同
        if (oldPassword.equals(newPassword)) {
            Toast.makeText(requireContext(), "新密码不能与原密码相同", Toast.LENGTH_SHORT).show();
            return;
        }

        // 获取用户ID
        long userId = preferenceManager.getUserId();

        // 验证原密码
        if (databaseHelper.verifyPassword(userId, oldPassword)) {
            // 更新密码
            if (databaseHelper.updatePassword(userId, newPassword)) {
                Toast.makeText(requireContext(), "密码修改成功", Toast.LENGTH_SHORT).show();
                try {
                    // 使用Navigation组件返回到个人中心
                    Navigation.findNavController(view).navigate(R.id.action_changePassword_to_profile);
                } catch (Exception e) {
                    // 如果导航失败，使用传统方式
                    requireActivity().getSupportFragmentManager().popBackStack();
                }
            } else {
                Toast.makeText(requireContext(), "密码修改失败，请重试", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(requireContext(), "原密码错误", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (databaseHelper != null) {
            databaseHelper.close();
        }
    }
} 